package com.android.kotlincoroutinemvvmdemo.glideoptions
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import com.android.kotlincoroutinemvvmdemo.R
import com.bumptech.glide.GenericTransitionOptions
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.annotation.GlideExtension
import com.bumptech.glide.annotation.GlideOption
import com.bumptech.glide.annotation.GlideType
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.BaseRequestOptions
import jp.wasabeef.glide.transformations.BitmapTransformation
import jp.wasabeef.glide.transformations.BlurTransformation

@GlideExtension
class MyGlideExtension private constructor() {
    companion object {

        private val DECODE_TYPE_BITMAP = GlideOptions.decodeTypeOf(Bitmap::class.java).lock()
        private val DECODE_TYPE_DRAWABLE = GlideOptions.decodeTypeOf(Drawable::class.java).lock()

        @JvmStatic
        @GlideOption
        fun applyAvatar(options: BaseRequestOptions<*>, size: Int): BaseRequestOptions<*> {

            val blurTransformation = BlurTransformation(25, 2)

            return options
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .override(size)
                //.centerCrop()
                //.fitCenter()
                //.circleCrop() //CircleCrop本身也是一种Transformation
                .transform(MultiTransformation(blurTransformation, CircleCrop()))
        }

        @JvmStatic
        @GlideType(Bitmap::class)
        fun createBitmap(builder: RequestBuilder<Bitmap>): RequestBuilder<Bitmap> {
            return builder.transition(GenericTransitionOptions.withNoTransition())
                .apply(DECODE_TYPE_BITMAP)
        }

        @JvmStatic
        @GlideType(Drawable::class)
        fun createDrawable(builder: RequestBuilder<Drawable>): RequestBuilder<Drawable> {
            return builder.transition(GenericTransitionOptions.withNoTransition())
                .apply(DECODE_TYPE_DRAWABLE)
        }
    }
}